//
//  SimpleNetWork.m
//  HelloMusic
//
//  Created by mac on 15/3/19.
//  Copyright (c) 2015年 gpr. All rights reserved.
//

#import "MSSimpleNetWork.h"

/** 请求超时时间 */
static NSTimeInterval timeOut = 15;

@implementation MSSimpleNetWork

+ (void)requestJSONWithURL:(NSString *)urlString complete:(void(^)(id jsonData, NSError *error))complete{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:0 timeoutInterval:timeOut];
   [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
       dispatch_main_async_safe(^ {
           if (complete) {
               complete(data,error);
           }
       });
       
   }] resume];
}

+ (void)requestJSONWithURL:(NSString *)urlString completeToCurrThread:(void(^)(id jsonData, NSError *error))complete{
    
    dispatch_queue_t currQueue = dispatch_get_current_queue();
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:0 timeoutInterval:timeOut];
    [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (complete) {
            dispatch_async(currQueue, ^{
                complete(data,error);
            });
        }
    }] resume];
}

@end
